package com.registration.controller;

import com.registration.Annotation.OperateLog;
import com.registration.bean.OrderBean;
import com.registration.service.OrderService;
import com.registration.util.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.fasterxml.jackson.databind.type.LogicalType.Map;

@RestController
@RequestMapping("/api/v1")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 获取订单列表
     * 对应文档：GET /api/v1/registration
     */
    @GetMapping("/registration")
    @OperateLog("获取挂号列表")
    public JsonResponse getOrderList(
            @RequestParam Integer user_id,
            @RequestParam(required = false) Integer order_status) {
        List<OrderBean> orders = orderService.getOrdersByUserId(user_id, order_status);
        return JsonResponse.success(orders);
    }

    /**
     * 获取订单详情
     * 对应文档：GET /api/v1/registration/{id}
     */
    @GetMapping("/registration/{orderId}")
    @OperateLog("获取订单详情")
    public JsonResponse getOrderDetail(@PathVariable Integer orderId) {
        try {
            OrderBean order = orderService.selectOrderDetailById(orderId);
            if (order == null) {
                return JsonResponse.fail("订单不存在或已删除");
            }
            return JsonResponse.success(order);
        } catch (Exception e) {
            return JsonResponse.fail("查询订单详情失败: " + e.getMessage());
        }
    }


    /**
     * 取消挂号
     * 对应文档：POST /api/v1/registration/{id}/cancel
     */
    @PostMapping("/registration/{orderId}/cancel")
    @OperateLog("取消挂号")
    public JsonResponse cancelRegistration(
            @PathVariable Integer orderId,
            @RequestParam Integer user_id) {
        boolean success = orderService.cancelOrder(orderId, user_id);
        return success ? JsonResponse.success(true, "取消成功") : JsonResponse.fail("取消失败，订单状态异常");
    }

    /**
     * 挂号改期
     * 对应文档：POST /api/v1/registration/{id}/reschedule
     */
    @PostMapping("/registration/{orderId}/reschedule")
    @OperateLog("挂号改期")
    public JsonResponse rescheduleRegistration(
            @PathVariable Integer orderId,
            @RequestParam Integer new_schedule_id,
            @RequestParam Integer user_id) {
        try {
            boolean success = orderService.rescheduleOrder(orderId, new_schedule_id, user_id);
            return success ? JsonResponse.success(true, "改期成功") : JsonResponse.fail("改期失败");
        } catch (RuntimeException e) {
            return JsonResponse.fail(e.getMessage());
        }
    }



    @GetMapping("/visited")
    @OperateLog("获取已就诊订单")
    public JsonResponse getVisitedOrders(@RequestParam Integer user_id) {
        List<OrderBean> orderBeanList=orderService.selectVisitedOrders(user_id);
        if(orderBeanList==null){
            return JsonResponse.fail("无已就诊订单");
        }else{
            return JsonResponse.success(orderBeanList);
        }
    }


    /**
     * 更新订单的症状描述和支付状态为1
     * @param orderId 订单ID
     * @param orderUpdates 更新的订单信息
     * @return 更新结果
     */
    @PutMapping("/registration/{orderId}")
    @OperateLog("更新订单")
    public JsonResponse updateOrder(
            @PathVariable Integer orderId,
            @RequestBody OrderBean orderUpdates) {
        try {
            int rows = orderService.updateOrder(orderId, orderUpdates);
            return rows > 0
                    ? JsonResponse.success(true, "更新成功")
                    : JsonResponse.fail("更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResponse.fail("更新失败: " + e.getMessage());
        }
    }

}